package com.xjtu.platform.reader.dao;

import com.xjtu.platform.reader.dto.BorrowHisDto;
import com.xjtu.platform.reader.dto.CancelReserveDto;
import com.xjtu.platform.reader.dto.ReserveBookDto;
import com.xjtu.platform.reader.dto.ReserveListDto;
import com.xjtu.platform.reader.entity.BookBaseInfo;
import com.xjtu.platform.reader.entity.ReserveListEntity;
import com.xjtu.platform.reader.entity.SatisfyReserve;
import com.xjtu.platform.reader.entity.UserPenaltyList;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface ReserveDao {

    @Insert("insert into book_reader_reserve_his " +
            "values(#{dto.book_id},#{id},null,#{time},null,#{dto.reserve_num},#{dto.borrow_time},'0')")
    void reserveBook(@Param("dto") ReserveBookDto reserveBookDto,@Param("id") String readerId,@Param("time") Timestamp nowTime);

    @Select("select current_num from book where book_id = #{bookId}")
    String getBookCurrentNum(String bookId);

    @Select("select * from book_reader_reserve_his where book_id = #{id} and (cast(reserve_num AS SIGNED) <= cast(#{num} as signed)) and reserve_state = '0' ORDER BY start_time ASC " +
            "limit 1")
    SatisfyReserve getSatisfiedReserveRecord(@Param("id") String bookId, @Param("num") String bookCurrentNum);

    @Update("update book_reader_reserve_his set end_time = #{time} , reserve_state = '1' where reserve_his_id = #{id} and reserve_state = '0'")
    void handleReserveIfSatisfied(@Param("id") String reserveHisId,@Param("time") Timestamp nowTime);


    @Select("select * from book_reader_reserve_his where reader_id = #{dto.reader_id} " +
            "and book_id in (select book_id from book where book_name like #{dto.book_name} and ISBN like #{dto.isbn} and book_id in (SELECT book_id from author_book,author where author_name like #{dto.author_name} and author.author_id = author_book.author_id) and " +
            "book_id in (SELECT book_id from book_class,class where class_name like #{dto.class_name} and class.class_id = book_class.class_id) and " +
            "book_id in (SELECT book_id from book_lable,library_label where label_name like #{dto.label_name} and book_lable.label_id = library_label.label_id) ) " +
            "ORDER BY start_time ASC ")
    List<ReserveListEntity> getReserveHisList(@Param("dto") ReserveListDto readerId);///////////////

    @Select("select * from book where book_id = #{bookId}")
    BookBaseInfo getReserveBookBaseInfo(String bookId);

    @Delete("delete from book_reader_reserve_his where reserve_his_id = #{reserve_his_id} and reserve_state = '0'")
    void cancelReserveById(CancelReserveDto cancelReserveDto);

    @Select("select user_penalty_id,money,time,book_reader_his_id from user_penalty where book_reader_his_id in " +
            "(select book_reader_his_id from reader_book_borrow_his where reader_id = #{reader_id})")
    List<UserPenaltyList> getPenaltyList(BorrowHisDto borrowHisDto);

    @Select("select book_name from book,reader_book_borrow_his where book.book_id = reader_book_borrow_his.book_id and book_reader_his_id = #{bookReaderHisId}")
    String getBookName(String bookReaderHisId);
}
